package com.kancy.jemeter.functions;

import com.kancy.jemeter.util.IDCardUtil;
import com.kancy.jemeter.util.MessageUtil;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;

import java.util.LinkedList;
import java.util.List;

/**
 * @author kancy
 * @date 2019/6/29 17:17
 */
public class AddressFunction extends JemeterAbstractFunction {

	public AddressFunction() {
		super(0,2);
	}

	@Override
	public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {
		String districtcode = IDCardUtil.getdistrictcode();
		while("0000".equals(districtcode.substring(2,6))
				|| "00".equals(districtcode.substring(4,6))){
			districtcode = IDCardUtil.getdistrictcode();
		}
		String address = IDCardUtil.parseAddress(districtcode);
		String suffix = "";
		if(hasParameter(0)){
			suffix = getParameter(0);
		}
		address = address + suffix;
		setJMeterVariable(1, address);
		return address;
	}

	private static final List<String> argumentDesclist = new LinkedList();

	static {
		argumentDesclist.add(MessageUtil.getResString("__address.suffix.message"));
		argumentDesclist.add(MessageUtil.getResString("varname.message"));
	}
	@Override
	public List<String> getArgumentDesc() {
		return argumentDesclist;
	}

}
